﻿@using System.Globalization
@using Smartstore.Utilities

@model object

@{
    if (Model == null || !Model.GetType().IsNumericType() || Model.GetType().IsGenericType)
    {
        //throw new InvalidOperationException("The model item to be used in a range editor must be numeric and non-nullable.");
    }

    var containerCssClass = "range-slider edit-control";
    if (ViewData.ContainsKey("size"))
    {
        containerCssClass += " range-slider-" + ViewData["size"].ToString();
    }
    if (ViewData.ContainsKey("class"))
    {
        containerCssClass += " " + ViewData["class"].ToString();
    }

    var sliderCssClass = "form-control-range px-0 custom-range";

    var name = ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty);
    var id = Html.GenerateIdFromName(name);

    var min = (GetMetadata<decimal?>("min") ?? 0m).ToString(CultureInfo.InvariantCulture);
    var max = (GetMetadata<decimal?>("max") ?? 100m).ToString(CultureInfo.InvariantCulture);
    var step = (GetMetadata<decimal?>("step") ?? 1m).ToString(CultureInfo.InvariantCulture);
    var ticks = GetMetadata<string>("ticks").SplitSafe(',').Select(x => x.Trim()).ToArray();
    var format = GetMetadata<string>("format") ?? "{0}";

    var invariantValue = ViewData.Model.Convert<decimal>().ToString(CultureInfo.InvariantCulture);
    var attributes = new AttributeDictionary().Merge(ConvertUtility.ObjectToDictionary(ViewData["htmlAttributes"] ?? new object()));
}

<div class="@containerCssClass" data-format="@Html.Raw(format)" data-editor="range">
    @*TODO: (mh) (core) Default validation was inserted by this*@
    @*@Html.Hidden(string.Empty, ViewData.Model)*@

    <input type="hidden" asp-for="@Model" attrs="attributes" />
    <input type="range" class="@sliderCssClass" data-target="#@id" min="@min" max="@max" step="@step" value="@invariantValue" />

    <div class="range-value" data-placement="left">
        <div class="arrow"></div>
        <div class="range-value-inner text-nowrap"></div>
    </div>

    <div sm-if="ticks.Length > 0" class="range-ticks">
        @foreach (var tick in ticks)
        {
            <div class="range-tick" data-value="@Html.Raw(tick)"></div>
        }
    </div>
</div>